#include "module_window_property.h"
#include "ui_module_window_property.h"

//#include "mainwindow.h"

#include <QDebug>

#include "GlobalSingal.h"

module_window_property::module_window_property(Module_Data *data, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::module_window_property)
{
    ui->setupUi(this);

    this->setObjectName("Property");
    //setCursor(Qt::PointingHandCursor);

    this->data = data;

    this->type = Module_Data::ModuleType::isModule;

    this->init();



#if (QT_VERSION <= QT_VERSION_CHECK(5,13,0))
    ui->lineEdit_Name->setValidator(new QRegExpValidator(QRegExp("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
#else //qt6版本
    ui->lineEdit_Name->setValidator(new QRegularExpressionValidator(QRegularExpression("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
    ///---------------------------------------------------------------
#endif
}

module_window_property::module_window_property(Child_Module_Data *data, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::module_window_property)
{
    ui->setupUi(this);

    this->setObjectName("Property");
    //setCursor(Qt::PointingHandCursor);

    this->child_data = data;

    this->type = Module_Data::ModuleType::isChildModule;

    this->init();



#if (QT_VERSION <= QT_VERSION_CHECK(5,13,0))
    ui->lineEdit_Name->setValidator(new QRegExpValidator(QRegExp("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
#else //qt6版本
    ui->lineEdit_Name->setValidator(new QRegularExpressionValidator(QRegularExpression("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
    ///---------------------------------------------------------------
#endif
}

module_window_property::~module_window_property()
{
    delete ui;
}

void module_window_property::init()
{
    if (type == Module_Data::ModuleType::isModule)
    {
        this->set_name(data->get_module_name());
        this->set_type(data->get_module_type());
        this->set_class(data->get_module_class());
    }
    else
    {
        this->set_name(child_data->instantiation_name);
        this->set_type(child_data->parent->get_module_type());
        this->set_type(child_data->parent->get_module_class());
    }

}

void module_window_property::set_name(QString name)
{
    ui->lineEdit_Name->setText(name);
}

void module_window_property::set_type(QString type)
{
    ui->label_type->setText(type);
}

void module_window_property::set_class(QString cls)
{
    ui->label_class->setText(cls);
}

void module_window_property::on_lineEdit_Name_textChanged(const QString &arg1)
{
    if (type == Module_Data::ModuleType::isModule) {
        this->data->set_module_name(arg1);
        GSingal.send_module_name_change(this->data);    // 广播 module name 发生改变
    }
    else {
        this->child_data->instantiation_name = arg1;
    }

    emit this->changeLabelName(arg1);  //发送Lable标签更改的信号给module_window
}

